using System;
using System.Windows;
using Phoenix.Diagnostics;
using Phoenix.Framework;

namespace Phoenix.Frames
{
    /// <summary>
    /// Used for a root visual which doesn't have a navigation frame. Allowing that root visual to contain one or many PartialView's or NavigationFrames
    /// </summary>
    public class FrameHost : INavigationFrameControl
    {
        readonly ContentFrameController contentFrameController;
        readonly string unableToNavigate;
        Window window;

        public FrameHost()
        {
            contentFrameController = new ContentFrameController(this);
            //Fake navigation to initial content
            unableToNavigate = "FrameHost is unable to navigate. Make sure the ViewModel which is trying to navigate is inside a PartialView or a NavigationFrame";
        }

        public void Initialise(Window windowHost)
        {
            window = windowHost;
            contentFrameController.NavigateTo(new NavigationRequest(null, null, window, null, InvokeOptions.None));            
        }

        public void NavigateBack()
        {
            window.Close();
        }

        public void NavigateBackTo(string controller, string action)
        {
            Trace.WriteDebug(this, () => unableToNavigate);
        }

        public void NavigateBackTo(string assembly, string controller, string action)
        {
            Trace.WriteDebug(this, () => unableToNavigate);
        }

        public void NavigateTo(NavigationRequest navigationRequest)
        {
            Trace.WriteDebug(this, () => unableToNavigate);
        }

        public void BeginOperation(InvokeActionRequest invokeRequest) { }

        public void EndOperation(InvokeActionRequest invokeRequest) { }

        public IFrameJournal Journal
        {
            get { return null; }
        }

        public bool CanGoBack
        {
            get { return false; }
        }

        public string Name
        {
            get { return window.Name; }
        }

        public NavigationRequest CurrentRequest
        {
            get { return contentFrameController.CurrentRequest; }
        }

        public void RegisterChild(IContentFrame targetFrame)
        {
            contentFrameController.RegisterChild(targetFrame);
        }

        public void DeactivateCurrentRequest()
        {
            contentFrameController.DeactivateCurrentRequest();
        }

        public void ActivateCurrentRequest()
        {
            contentFrameController.ActivateCurrentRequest();
        }

        public void InitialiseHost(IPhoenixHost host)
        {
            contentFrameController.InitialiseHost(host);
        }

        public void InvalidateChildren()
        {
            contentFrameController.InvalidateChildren();
        }

        public ChildFrameCollection ChildFrames
        {
            get { return contentFrameController.ChildFrames; }
        }

        public IContentFrame ParentFrame
        {
            get { return null; }
        }

        public void SetParent(IContentFrame parentFrame){}
        public event NavigationCompleteEvent NavigationComplete
        {
            add { contentFrameController.NavigationComplete += value; }
            remove { contentFrameController.NavigationComplete -= value; }
        }
        public event EventHandler ForegroundOperationStarted
        {
            add { contentFrameController.ForegroundOperationStarted += value; }
            remove { contentFrameController.ForegroundOperationStarted -= value; }
        }
        public event EventHandler ForegroundOperationComplete
        {
            add { contentFrameController.ForegroundOperationComplete += value; }
            remove { contentFrameController.ForegroundOperationComplete -= value; }
        }

        public void CurrentRequestClosing(RequestClosingEventArgs eventArgs)
        {
            contentFrameController.CurrentRequestClosing(eventArgs);
        }

        public void Dispose()
        {
            contentFrameController.Dispose();
        }

        public bool IsContentEnabled { get; set; }
        public double ContentOpacity { get; set; }
        public bool ShowProgressBar { get; set; }
        public Visibility ProgressBarVisibility { get; set; }
        public string LoadingMessage { get; set; }
        public object Content { get; set; }
        public void ProgressIndicatorVisible(bool isVisible){}
    }
}